package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogTalkBo;
import com.an.blog.admin.domain.vo.BlogTalkVo;
import com.an.blog.service.IBlogTalkService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 博客说说
 *
 * @author: Ann
 * @createDate: 2023/05/10 13:48:43
 * @version: 1.0.0
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/talk")
public class BlogTalkController extends BaseController {

    private final IBlogTalkService blogTalkService;

    /**
     * 查询博客说说列表
     */
    @SaCheckPermission("blog:talk:list")
    @GetMapping("/list")
    public TableDataInfo<BlogTalkVo> list(BlogTalkBo bo, PageQuery pageQuery) {
        return blogTalkService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取博客说说详细信息
     *
     * @param talkId 主键
     */
    @SaCheckPermission("blog:talk:query")
    @GetMapping("/{talkId}")
    public R<BlogTalkVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long talkId) {
        return R.ok(blogTalkService.queryById(talkId));
    }

    /**
     * 新增博客说说
     */
    @SaCheckPermission("blog:talk:add")
    @Log(title = "博客说说", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogTalkBo bo) {
        return toAjax(blogTalkService.insertByBo(bo));
    }

    /**
     * 修改博客说说
     */
    @SaCheckPermission("blog:talk:edit")
    @Log(title = "博客说说", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogTalkBo bo) {
        return toAjax(blogTalkService.updateByBo(bo));
    }


    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/10 17:10:29
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:talk:edit")
    @Log(title = "说说管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogTalkBo bo) {
        return toAjax(blogTalkService.updateArticleStatus(bo));
    }

    /**
     * 置顶修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:50:26
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:talk:edit")
    @Log(title = "说说管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeIsTop")
    public R<Void> changeIsTop(@RequestBody BlogTalkBo bo) {
        return toAjax(blogTalkService.updateArticleIsTop(bo));
    }

    /**
     * 删除博客说说
     *
     * @param talkIds 主键串
     */
    @SaCheckPermission("blog:talk:remove")
    @Log(title = "博客说说", businessType = BusinessType.DELETE)
    @DeleteMapping("/{talkIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] talkIds) {
        return toAjax(blogTalkService.deleteWithValidByIds(Arrays.asList(talkIds), true));
    }
}
